package cn.lxg.controller.admin;

import cn.lxg.pojo.TComment;
import cn.lxg.pojo.TNovel;
import cn.lxg.pojo.TNovelChapter;
import cn.lxg.service.ICommentService;
import cn.lxg.service.INovelChapterService;
import cn.lxg.service.INovelService;
import cn.lxg.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller(value = "adminNovelChapterController")
@RequestMapping(value = "/page/admin")
public class NovelChapterController {

    @Autowired
    private final INovelChapterService novelChapterService = null;

    @Autowired
    private final INovelService novelService = null;

    @GetMapping("/novel_chapter_list")
    public String listPage(
            Model model,
            @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            TNovelChapter novelChapter) {
        // 预处理参数
        if (novelChapter.getName() != null)
            novelChapter.setName(novelChapter.getName().trim());
        TNovel novel = new TNovel();

        // 准备数据
        List<TNovelChapter> novelChapterList = this.novelChapterService.getList(limit, page, novelChapter);
        List<TNovel> novelList = this.novelService.getList0(null, null, novel);
        int dataCount = this.novelChapterService.countDataLine(novelChapter);
        int pageIndex = page;
        int pageCount = (int) Math.ceil((float) dataCount / limit);

        // 传递数据
        model.addAttribute("novelChapter", novelChapter);
        model.addAttribute("novelChapterList", novelChapterList);
        model.addAttribute("novelList", novelList);
        model.addAttribute("dataCount", dataCount);
        model.addAttribute("defaultLimit", limit);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageCount", pageCount);

        return "admin/novelChapter/list";
    }

    @GetMapping(value = "/add_novel_chapter")
    public String addPage(Model model) {
        // 预处理参数
        TNovel novel = new TNovel();

        // 准备数据
        List<TNovel> novelList = this.novelService.getList(null, null, novel);

        // 传递数据
        model.addAttribute("novelList", novelList);

        return "admin/novelChapter/add";
    }

    @PostMapping(value = "/novel_chapter")
    public String add(Model model, TNovelChapter novelChapter) {
        // 参数预处理
        novelChapter.setName(novelChapter.getName().trim());

        // 准备数据
        String msg = this.novelChapterService.add(novelChapter);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }



    @GetMapping(value = "/delete_novel_chapter/{id}")
    public String deletePage(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        TNovelChapter novelChapter = this.novelChapterService.get(id);

        // 传递数据
        model.addAttribute("novelChapter", novelChapter);

        return "admin/novelChapter/delete";
    }

    @DeleteMapping(value = "/novel_chapter/{id}")
    public String delete(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        String msg = this.novelChapterService.delete(id);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    @GetMapping(value = "/edit_novel_chapter/{id}")
    public String editPage(Model model, @PathVariable(value = "id") Integer id) {
        // 预处理参数
        TNovel novel = new TNovel();

        // 准备数据
        TNovelChapter novelChapter = this.novelChapterService.get(id);
        List<TNovel> novelList = this.novelService.getList(null, null, novel);

        // 传输数据给前端
        model.addAttribute("novelChapter", novelChapter);
        model.addAttribute("novelList", novelList);

        return "admin/novelChapter/edit";
    }

    @PutMapping(value = "/novel_chapter")
    public String edit(Model model, TNovelChapter novelChapter) {
        // 参数预处理
        novelChapter.setName(novelChapter.getName().trim());

        // 准备数据
        String msg = this.novelChapterService.edit(novelChapter);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    @GetMapping(value = "/look_novel_chapter/{id}")
    public String lookPage(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        TNovelChapter novelChapter = this.novelChapterService.get(id);

        // 传输数据给前端
        model.addAttribute("novelChapter", novelChapter);

        return "admin/novelChapter/look";
    }
}
